// var r = http.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Obo74pWYA2dQ1gs992g9ufvr&client_secret=A0jCw00ZC2XfzZE8kplZGN7EhoOmqeDk");

// log("code = " + r.statusCode);
// var body = r.body.json();
// log("html = " + body);
// log("html = " + body.access_token);
// if(1 == 1){
//     exit();
// }

var myWord = {};

myWord.userBaidu = false;

myWord.isLog = false;


myWord.getAllword = function () {
    try {
        if (myWord.userBaidu) {
            return baiduOcr();
        } else {
            return localOcr();
        }
    } catch (error) {
        myWord.userBaidu = true;
        threads.start(function () {
            sleep(480000)
            myWord.userBaidu = false;
        });
        return baiduOcr();
    }
}

function localOcr() {
    var date = new Date();
    var img = getCaptureImg();
    var day = new Date();
    log("截图", day - date);
    var result = paddle.ocr(img, 8)
    date = new Date();
    log("识别", date - day);
    if (myWord.isLog) {
        log(JSON.stringify(result))
    }
    return result;
}

function baiduOcr() {

    var date = new Date();
    var img = getCaptureImg();
    var day = new Date();
    log("截图", day - date);
    var imgBase = images.toBase64(img);
    date = new Date();
    log("转base", date - day);
    // log(imgBase.length)
    // var imgEncode = encodeURIComponent(imgBase);
    // log(imgEncode.length)

    var access_token = "24.8233d0f1c5cd47638c54af6ca593e116.2592000.1646913338.282335-25243556";

    var url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + access_token;
    var res = http.post(url, {
        "image": imgBase
    }, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }

    });
    day = new Date();
    log("http", day - date);
    var a = res.body;
    a = a.json();
    // log(res.body.string());
    if (myWord.isLog) {
        log(a.words_result)
    }
    return a.words_result;
}

myWord.clickWord = function (text, wordRes, idx, fuzzy) {
    var location = myWord.findWordLocation(text, wordRes, idx, fuzzy);
    if (location != null) {
        return this.clickLocation(location)
    }
    log("没找到", text);
    return false;
}

myWord.clickLocation = function (location) {
    var top = parseInt(location.top);
    var left = parseInt(location.left);
    var width = parseInt(location.right) - left;
    var height = parseInt(location.bottom) - top;
    var centerX = left + width / 2;
    var centerY = top + height / 2;
    click(centerX, centerY)
    log("点", centerX, centerY);
    return true;
}

myWord.wordExist = function (text, wordRes, idx, fuzzy) {
    var location = myWord.findWordLocation(text, wordRes, idx, fuzzy);
    if (location != null) {
        return true;
    }
    return false;
}


function prepareThings() {
    //请求截图
    threads.start(function () {
        text("立即开始").findOne(3000).click();
    });
    if (!requestScreenCapture()) {
        toast("请求截图失败");
        exit();
    }
    sleep(1000);
}


//获取截图
function getCaptureImg() {
    try {
        var img0 = captureScreen();
        sleep(100);
        if (img0 == null || typeof (img0) == "undifined") {
            myFunc.log("截图失败,脚本退出");
            exit();
        } else {
            return img0;
        }
    } catch (error) {
        prepareThings();
        return getCaptureImg();
    }

}

myWord.find = function (text) {
    var result = [];
    var wordRes = myWord.getAllword();
    for (var index = 0; index < wordRes.length; index++) {
        var element = wordRes[index];
        var word = element.words;
        if (word.indexOf(text) != -1) {
            result.push(element);
        }
    }
    return result;
}

myWord.findOne = function (word, tryAgainTime, idx, fuzzy) {
    if (tryAgainTime == undefined) {
        tryAgainTime = 1;
    }
    while (tryAgainTime > 0) {
        var retsult = myWord.findWordLocation(word, null, idx, fuzzy);
        if (retsult != null) {
            return retsult;
        }
        sleep(1000);
        tryAgainTime = tryAgainTime - 1;
    }
    return null;
}

myWord.findClickWord = function (text, idx, fuzzy) {
    var result = this.getAllword();
    return this.clickWord(text, result, idx, fuzzy);
}

myWord.findOneClick = function (word, tryAgainTime, idx, fuzzy) {
    if (tryAgainTime == undefined) {
        tryAgainTime = 1;
    }
    while (true) {
        if (myWord.findClickWord(word, idx, fuzzy)) {
            return true;
        }
        tryAgainTime = tryAgainTime - 1;
        if (tryAgainTime > 0) {
            sleep(1000);
        } else {
            break;
        }
    }
    return false;
}


myWord.findWordLocation = function (text, wordRes, idx, fuzzy) {
    if (wordRes == undefined || wordRes == null) {
        wordRes = this.getAllword();
    }
    if (idx == undefined || idx == null) {
        idx = 1;
    }
    if (fuzzy == undefined || fuzzy == null) {
        fuzzy = true;
    }
    for (var index = 0; index < wordRes.length; index++) {
        var element = wordRes[index];
        var word = element.words;
        if (fuzzy) {
            if (word.indexOf(text) != -1) {
                if (idx > 1) {
                    idx = idx - 1;
                } else {
                    return element.bounds;
                }
            }
        } else if (word == text) {
            if (idx > 1) {
                idx = idx - 1;
            } else {
                return element.bounds;
            }
        }

    }
    return null;
}


module.exports = myWord;